package com.tfswue.lhj.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tfswue.lhj.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户Mapper接口
 * 
 * @author daygods
 * @since 2025-07-09
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {
    
    /**
     * 根据用户名查询用户
     * 
     * @param username 用户名
     * @return 用户信息
     */
    @Select("SELECT * FROM sys_user WHERE username = #{username} AND deleted = 0")
    User findByUsername(@Param("username") String username);
    
    /**
     * 根据邮箱查询用户
     * 
     * @param email 邮箱
     * @return 用户信息
     */
    @Select("SELECT * FROM sys_user WHERE email = #{email} AND deleted = 0")
    User findByEmail(@Param("email") String email);
    
    /**
     * 根据手机号查询用户
     * 
     * @param phone 手机号
     * @return 用户信息
     */
    @Select("SELECT * FROM sys_user WHERE phone = #{phone} AND deleted = 0")
    User findByPhone(@Param("phone") String phone);
    
    /**
     * 更新用户登录信息
     * 
     * @param userId 用户ID
     * @param loginTime 登录时间
     * @param loginIp 登录IP
     * @return 更新行数
     */
    @Update("UPDATE sys_user SET last_login_time = #{loginTime}, last_login_ip = #{loginIp}, " +
            "login_count = IFNULL(login_count, 0) + 1, update_time = NOW() WHERE id = #{userId}")
    int updateLoginInfo(@Param("userId") Long userId, 
                       @Param("loginTime") LocalDateTime loginTime, 
                       @Param("loginIp") String loginIp);
    
    /**
     * 更新用户状态
     * 
     * @param userId 用户ID
     * @param status 状态
     * @return 更新行数
     */
    @Update("UPDATE sys_user SET status = #{status}, update_time = NOW() WHERE id = #{userId}")
    int updateStatus(@Param("userId") Long userId, @Param("status") Integer status);
    
    /**
     * 更新用户密码
     * 
     * @param userId 用户ID
     * @param password 新密码
     * @return 更新行数
     */
    @Update("UPDATE sys_user SET password = #{password}, update_time = NOW() WHERE id = #{userId}")
    int updatePassword(@Param("userId") Long userId, @Param("password") String password);
    
    /**
     * 分页查询用户列表
     * 
     * @param page 分页参数
     * @param username 用户名（模糊查询）
     * @param email 邮箱（模糊查询）
     * @param phone 手机号（模糊查询）
     * @param status 状态
     * @return 用户分页列表
     */
    @Select("<script>" +
            "SELECT * FROM sys_user WHERE deleted = 0" +
            "<if test='username != null and username != \"\">" +
            " AND username LIKE CONCAT('%', #{username}, '%')" +
            "</if>" +
            "<if test='email != null and email != \"\">" +
            " AND email LIKE CONCAT('%', #{email}, '%')" +
            "</if>" +
            "<if test='phone != null and phone != \"\">" +
            " AND phone LIKE CONCAT('%', #{phone}, '%')" +
            "</if>" +
            "<if test='status != null'>" +
            " AND status = #{status}" +
            "</if>" +
            " ORDER BY create_time DESC" +
            "</script>")
    IPage<User> selectUserPage(Page<User> page, 
                              @Param("username") String username,
                              @Param("email") String email,
                              @Param("phone") String phone,
                              @Param("status") Integer status);
    
    /**
     * 查询指定状态的用户数量
     * 
     * @param status 状态
     * @return 用户数量
     */
    @Select("SELECT COUNT(*) FROM sys_user WHERE status = #{status} AND deleted = 0")
    long countByStatus(@Param("status") Integer status);
    
    /**
     * 查询今日注册用户数量
     * 
     * @return 今日注册用户数量
     */
    @Select("SELECT COUNT(*) FROM sys_user WHERE DATE(create_time) = CURDATE() AND deleted = 0")
    long countTodayRegistered();
    
    /**
     * 查询最近登录的用户列表
     * 
     * @param limit 限制数量
     * @return 用户列表
     */
    @Select("SELECT * FROM sys_user WHERE last_login_time IS NOT NULL AND deleted = 0 " +
            "ORDER BY last_login_time DESC LIMIT #{limit}")
    List<User> findRecentLoginUsers(@Param("limit") int limit);
    
    /**
     * 检查用户名是否存在（排除指定用户ID）
     * 
     * @param username 用户名
     * @param excludeUserId 排除的用户ID
     * @return 是否存在
     */
    @Select("SELECT COUNT(*) > 0 FROM sys_user WHERE username = #{username} " +
            "AND id != #{excludeUserId} AND deleted = 0")
    boolean existsUsernameExcludeId(@Param("username") String username, 
                                   @Param("excludeUserId") Long excludeUserId);
}